package THREADS;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import DeportManage.Customer;
import DeportManage.CustomerList;
import DeportManage.DuplicateException;
import DeportManage.ErrorFormatNameException;
import DeportManage.Name;
import DeportManage.NotFormatParcelIDException;
import LOG.Logger;
import MVC.DataObserver;
import MVC.Observer;

public class CustomerThreads extends Thread implements Observer{
	private DataObserver data_observer;
	private CustomerList customer_list;
	private int seq_No;
	/**
	 * Constructor to create an object of customer thread with Observer 
	 * @param data_observer an object of Observer interface
	 */
	public CustomerThreads(DataObserver data_observer){
		this.data_observer = data_observer;
		data_observer.registerObserver(this);
	}
	/**
	 * Method to run the thread methods
	 */
	@Override
	public void run() {	
		update();
		int count = 1;
		
		try{
			File inputFile = new File("New Customer.txt");
			Scanner sc=new Scanner(inputFile);
				while(sc.hasNextLine()){
					String readingLine = sc.nextLine();
					if (readingLine.length() != 0){
						try{
							Thread.sleep(100);
							if(readline(readingLine) != null){
								data_observer.addCustomer(readline(readingLine));
								count++;
							}
						}
						catch(InterruptedException ie){
							Logger.writeLog(ie.getMessage());
						}
					}
				}
		}catch(FileNotFoundException fnf){//boolean whether there is the file.
			System.out.println("New Customer.txt is not found! ");
			System.exit(0);
		}		

		//data_observer.setInitalSize(count);	
		data_observer.setState(false);
	}
	/**
	 * Method to read customer information in a line
	 * @param readline a line of information for customer
	 * @return Customer the object of Customer class
	 */
	public Customer readline(String readline)
	{
		try{
			String customerstring [] = readline.split(",");
			Name name = new Name(customerstring[0]);	
			if(!customer_list.getCustomerList().isEmpty()){	
				int indexOfLastObject = customer_list.getSize()-1;
				Customer lastCustomer = customer_list.getCustomerList().get(indexOfLastObject);
				seq_No = lastCustomer.getSequenceNo()+1;
			}
			String id_parcel = customerstring[1];
			@SuppressWarnings("unused")
			int numberOfID = Integer.parseInt(id_parcel.substring(1));
			Customer customer = new Customer(seq_No,name,id_parcel);	
			
			return customer;
		}
		catch(ArrayIndexOutOfBoundsException oae){
			System.out.println(oae.getMessage());
		} catch (ErrorFormatNameException efne) {
			System.out.println(efne.getMessage());
		} catch (NotFormatParcelIDException nfpie) {
			System.out.println(nfpie.getMessage());
		}
		
		return null;
	}	
	/**
	 * Method to update the size of customer list and the final sequence no. 
	 */
	@Override
	public void update() {
		customer_list = data_observer.getCustList();		
		if( data_observer.getCustList().getCustomerList().isEmpty()){
			seq_No =  data_observer.getSequnce_no();
		}
		
	}	 
}